<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title></title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link href="/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="/css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">


    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>用户管理<small>编辑</small></h5>
                </div>
                <div class="ibox-content">
                    <form method="post" action="/admin/user/doEdit" class="form-horizontal">
                        {{csrf_field()}}
                        <input type="hidden" name="info[id]" value="{{ $info->id }}"/>
                    @if($info->id != 1)
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" >角色：</label>
                            <div class="col-sm-10">
                                <div class="row">

                                    <div class="col-sm-10">
                                        @foreach($roles as $role)
                                            <label class="checkbox-inline i-checks">
                                                <input class="_forchecked" type="checkbox" name="roles[]" value="{{ $role['id'] }}"
                                                @if($info->hasRole($role['name'])) checked @endif
                                                >{{$role['label']}}</label>
                                        @endforeach
                                    </div>

                                </div>
                            </div>
                        </div>
                    @endif
                        <div class="hr-line-dashed"></div>
                        <div class="form-group" style="display: block" id="target-group">
                            <label class="col-sm-2 control-label">可执行权限：</label>
                            <div class="col-sm-10">
                                <div class="row">
                                    <div id="tab-1" style="height: 200px" >
                                        <div  id="loading"  style="display: none">
                                            <div class="sk-spinner sk-spinner-wave">
                                                <div class="sk-rect1"></div>
                                                <div class="sk-rect2"></div>
                                                <div class="sk-rect3"></div>
                                                <div class="sk-rect4"></div>
                                                <div class="sk-rect5"></div>
                                            </div>
                                        </div>
                                        <div class="full-height-scroll" id="target-list-container">
                                            <div class="col-sm-2"><label></label></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" >可访问页面元素：</label>
                            <div class="col-sm-10">
                                <div class="row">
                                    <div class="col-sm-12">

                                        <div class="row">
                                            <div id="tab-1" style="height: 200px" >
                                                <div id="items-loading"  style="display: none">
                                                    <div class="sk-spinner sk-spinner-wave">
                                                        <div class="sk-rect1"></div>
                                                        <div class="sk-rect2"></div>
                                                        <div class="sk-rect3"></div>
                                                        <div class="sk-rect4"></div>
                                                        <div class="sk-rect5"></div>
                                                    </div>
                                                </div>
                                                <div class="full-height-scroll" id="items-list-container">

                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <button class="btn btn-primary" type="submit">保存内容</button>
                                <a href="javascript:void(0);" onclick="window.history.back()">
                                <button class="btn btn-white" type="button">取消</button>
                                </a>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.6"></script>
<!-- iCheck -->
<script src="/js/plugins/iCheck/icheck.min.js"></script>
<!-- 自定义js -->
<script src="/js/content.js?v=1.0.0"></script>
<script src="/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>

<script src="/js/plugins/layer/layer.min.js"></script>
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
        getPermissList();
        $('.full-height-scroll').slimScroll({
            height: '100%'
        });

        $('._forchecked').on('ifChanged', function(event){ //ifCreated 事件应该在插件初始化之前绑定
            getPermissList();
        });
        function getPermissList() {
            var roles=[];
            var _template='<div class="col-sm-2"><label>NAME</label></div>';
            var _reg_name = /NAME/g;
            $("._forchecked:checked").each(function () {
                roles.push($(this).val());
            });
            $.ajax({
                type:'get',
                dataType:'json',
                data:{roles:roles},
                url:'/admin/role/getpermissionsByids',
                beforSend:function () {
                    $("#loading").show();
                },
                success:function (data) {
                    if(data.code==1){
                        var permissions =[];
                        $("#target-list-container").html("");
                        $.each(data.data.list,function (index,element) {
                            console.log(element);
                            var content =_template.replace(_reg_name,element.label);
                            $("#target-list-container").append(content);
                            permissions.push(element.id);
                        });
                       getItemsList(permissions);
                    }else{
                        layer.msg(data.message, {icon: 6});
                    }
                },
                complete:function () {
                    $("#loading").hide();
                }
            });
        }
        
        function getItemsList(permissions) {
            console.log(permissions);
            var _template =  '<label class="checkbox-inline i-checks2">'+
                        '<input type="checkbox" id="itemsid-IDNUMBER" name="items[]" value="VALUE" >NAME</label>';
            var _reg_name = /NAME/g;
            var _reg_value = /VALUE/g;
            var _reg_id = /IDNUMBER/g;
            $.ajax({
                type:'get',
                dataType:'json',
                data:{permissions:permissions},
                url:'/admin/viewItems/getItemsByPermissions',
                beforeSend:function () {
                    $("#items-loading").show();
                },
                success:function (data) {
                    if(data.code == 1){
                        $("#items-list-container").html('');
                        $.each(data.data.list,function (index,element) {
                            console.log(element);
                            var content =_template.replace(_reg_name,element.name);
                            content =content.replace(_reg_value,element.id);
                            content =content.replace(_reg_id,element.id);
                            $("#items-list-container").append(content);
                        });
                        $('.i-checks2').iCheck({
                            checkboxClass: 'icheckbox_square-green',
                            radioClass: 'iradio_square-green',
                        });
                        getUserCanViewItems();
                    }
                },
                complete:function () {
                    $("#items-loading").hide();
                }
            });
        }


        function getUserCanViewItems() {
            var userId =$("input[name='info[id]']").val();
            $.ajax({
                type:'get',
                dataType:'json',
                data:{userId:userId},
                url:'/admin/viewItems/getItemsByUser',
                success:function (data) {
                    console.log();
                    if(data.code == 1){
                        $.each(data.data.list,function (index,element) {
                            console.log(element);
                            $("#itemsid-"+element.id).iCheck('check');
                        });
                    }
                }
            });
        }
    });
</script>

</body>

</html>
